﻿using Ay.Framework.WPF.Controls;
using Ay.Framework.WPF.FuncFactory;
using Ay.MvcFramework.Config;
using Hotel.UI.Models;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;

namespace Hotel.UI.Views
{
    /// <summary>
    /// Login.xaml 的交互逻辑
    /// </summary>
    public partial class Login : AyWindow
    {
        public LoginModel model = new LoginModel();
        private AyLoginAccountManager ayLoginAccountManager;
        public ObservableCollection<LoginWindowAccountModel> observableCollectionAccounts = new ObservableCollection<LoginWindowAccountModel>();

        private void media_Loaded(object sender, RoutedEventArgs e)
        {
            (sender as MediaElement).Play();
        }
        private void media_MediaEnded(object sender, RoutedEventArgs e)
        {
            // MediaElement需要先停止播放才能再开始播放，
            // 否则会停在最后一帧不动
            (sender as MediaElement).Stop();
            (sender as MediaElement).Play();
        }
        private void media_Unloaded(object sender, RoutedEventArgs e)
        {
            (sender as MediaElement).Stop();
        }

        public Login()
        {
            InitializeComponent();
            // 交互式控制
            mediaelement.LoadedBehavior = MediaState.Manual;
            // 添加元素加载完成事件 -- 自动开始播放
            mediaelement.Loaded += new RoutedEventHandler(media_Loaded);
            // 添加媒体播放结束事件 -- 重新播放
            mediaelement.MediaEnded += new RoutedEventHandler(media_MediaEnded);
            // 添加元素卸载完成事件 -- 停止播放
            mediaelement.Unloaded += new RoutedEventHandler(media_Unloaded);

            mediaelement.Source = new Uri("ab.mp4", UriKind.Relative);
            mediaelement.Play();

            AYUI.Message.Register(this, MESSAGES.CloseLoginWiew, (string a) =>
            {

                AyWindowManager<Change>.Show(delegate (Change win)
                {
                    System.Windows.Application.Current.MainWindow = win;

                    win.Show();
                    Close();
                }, null);
            });

            model = new LoginModel();
            ayLoginAccountManager = new AyLoginAccountManager(AySerializeFilePath.LoginAccountInfo);
            List<LoginWindowAccountModel> list = ayLoginAccountManager.Load();
            if (list != null) list.Clear();
            if (list == null || list.Count < 1)
                list = new List<LoginWindowAccountModel>
              {
                  new LoginWindowAccountModel
                  {
                      Name = "18001",

                      Password = "123123",
                    HeadImgSrc=@"\Contents\Images\Header\1.png",
                      //Header="",
                     NickName="啵比.陈"
                  },
                  new LoginWindowAccountModel
                  {
                      Name = "18002",
                      Password = "123123",
                         HeadImgSrc=@"\Contents\Images\Header\2.png",
                      //Header="",
                     NickName="迪丽热巴",
                  },
                  new LoginWindowAccountModel
                  {
                      Name = "18003",
                      Password = "123456",
                    HeadImgSrc=@"\Contents\Images\Header\3.png",
                      //Header="",
                     NickName="马云",
                     },
                  new LoginWindowAccountModel
                  {
                      Name = "18004",
                      Password = "123123",
                        HeadImgSrc=@"\Contents\Images\Header\1.png",
                      //Header="",
                     NickName="杰克.陈",
                  },
                  new LoginWindowAccountModel
                  {
                      Name = "admin",
                      Password = "admin",
                      //Header="",
                    NickName="杰克.陈",
                  },
                  new LoginWindowAccountModel
                  {
                      Name = "superadmin",
                      Password = "superadmin",
                      //Header=""
                    NickName="杰克.陈",
                  },
                  new LoginWindowAccountModel
                  {
                      Name = "18005",
                      Password = "123123",
                      //Header=""
                    NickName="杰克.陈"
                  }
              };


            foreach (LoginWindowAccountModel item in list)
            {
                if (item.IsAutoLoginSystem || item.IsRememberPassword)
                {
                    item.Password = AyFuncFactory.GetFunc<AyFuncSecrity>().RSADecrypt(item.Password, "123456");
                }
            }
            observableCollectionAccounts = list.ToObservableCollection();
            if (AyCommon.IsNotNull((object)observableCollectionAccounts))
            {
                if (observableCollectionAccounts.Count == 0)
                {
                    txtAccount.PanelMinHeight = 100.0;
                }
                else
                {
                    LoginWindowAccountModel loginWindowAccountModel = observableCollectionAccounts.FirstOrDefault((LoginWindowAccountModel x) => x.IsSelected);
                    if (AyCommon.IsNotNull((object)loginWindowAccountModel))
                    {
                        model.AccountModel = loginWindowAccountModel;
                    }
                    txtAccount.SetBinding(ItemsControl.ItemsSourceProperty, new Binding
                    {
                        Source = observableCollectionAccounts
                    });
                }
            }
            else
            {
                txtAccount.PanelMinHeight = 100.0;
            }
            ayLoginAccountManager.Save(list);
            AyExtension.SetAyWindowMouseLeftButtonMove(this);
        }

        private void BtnLogin_Click(object sender, RoutedEventArgs e)
        {
            AyWindowManager<Change>.Show(delegate (Change win)
                                {
                                    System.Windows.Application.Current.MainWindow = win;

                                    win.Show();
                                    Close();
                                }, null);
        }
    }
}